var audio='audio';
var div_audio='div_'+audio; 
var div_audio_content=div_audio+'_content';
var form_audio='form_'+audio;
var a_upload_audio='a_upload_'+audio; 

var p_added_file='p_added_file';
var audio_progress='audio_progress';
var p_audio_info='p_audio_info';

function audio_div_content(){
	
	$('#'+div_audio).append(''+
		'<form id="'+form_audio+'" method="post"'+
		' 	action="./jsp/modules/audio.jsp"'+
		' 	enctype="multipart/form-data">'+
		'<table height="25px"><tr><td style="vertical-align:middle; ">'+
			'<img src="./images/music_note.ico" width="18px">'+
		'</td><td style="vertical-align:middle; width:90px;">'+
		'	<div style="position:relative; ">'+
		'	<div style="position:absolute; top:8px; '+
		'				left:0px; z-index:1; cursor:pointer; width:50px; ">'+
		'	&nbsp;<a id="a_add_audio">add audio</a>'+
		'	</div>'+
		'	<input type="file" name="myaudio" id="_audiofile" class="img_file_browser"'+
		' 	onchange="$(\'#'+p_added_file+'\').text(get_filename(this.value));"'+
		' 	style="width:30px">'+
		'	</div>'+	
		'</td><td style="vertical-align:middle; ">'+
		'	<a id="'+p_added_file+'">no file</a>&nbsp;'+
		'	<b><a id="'+a_upload_audio+'">upload</a></b>'+
		'</td><td style="vertical-align:middle;">'+
		'	&nbsp;&nbsp;'+
		'	<img id="'+audio_progress+'" src="./images/progress.gif">'+
		'</td><td style="vertical-align:middle;">'+
		'	&nbsp;<a id="'+p_audio_info+'"></a>'+
		'</td></tr>'+
		'</table>'+
		'</form>'+
		'');
	
	$('#'+a_upload_audio).click(function(){
		if ($('#_audiofile').attr('value')==''){
			alert('nothing to upload');
			return;
		}
		$('#'+form_audio).ajaxForm({
		    beforeSend: function() {
		    	$('#'+audio_progress).show('fast');
		    },
		    uploadProgress: function(
		    		event, position, total, percentComplete) {

		    },
			complete: function(xhr) {
				var ret=xhr.responseText.split(splitter);
				if (ret[0] != 1){
					alert('uploading audio failed' + '\n' + ret[1]);
				}else{
					add_audio_graph(ret[1],ret[2],ret[3]);
				}
				$('#'+audio_progress).hide('fast');
			}
		}).submit();		
	});
	
	$('#'+audio_progress).hide();
}

function add_audio_graph(thumbImg,audioFile,xdata){
	var data=xdata.replace('</nsoft_inline_splitter>','');
	$('#'+div_audio).css(
			'background-image','url('+thumbImg+')');
	
	$('#'+p_added_file).text('');
	var info=data.split(inline_splitter);
	
	var serie='';
	$.each(info,function(k,v){
		if (k==0)
			serie+=v.trim()+' ';
		else if (k==1)
			serie+='('+v.trim()+') | ';
		else if(k < info.length-1)
			serie+=v.trim()+' | ';
		else if (k == info.length-1)
			serie+=v.trim();
	});
	$('#'+p_audio_info).text(serie);
	
	var exdata='audio'+splitter+
			thumbImg+splitter+
			audioFile+splitter+
			data;
	contact_session(TYPE_REPLACE,
			ARRAY_AUDIO_SLIDE,null,exdata);
}

function add_audio_graph_auto(key,content){
	if (content=='')
		return;
	
	var e=content.trim().split(splitter);
	var thumbImg=e[1];
	/*
	var audioFile=e[2];
	*/
	$('#'+div_audio).css(
			'background-image','url('+thumbImg+')');
	
	$('#'+p_added_file).text('');
	
	var info=e[3].split(inline_splitter);
	var serie='';
	$.each(info,function(k,v){
		if (k==0)
			serie+=v.trim()+' ';
		else if (k==1)
			serie+='('+v.trim()+') | ';
		else if(k < info.length-2)
			serie+=v.trim()+' | ';
		else if (k == info.length-2)
			serie+=v.trim();
	});
	$('#'+p_audio_info).text(serie);
}

function get_filename(filepath){
	return filepath.substring(
			filepath.lastIndexOf('\\')+1);
}

